package com.tq.util;

import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SubListUtil<T> {

    /**
     * 利用subList方法进行分页
     *
     * @param list        分页数据
     * @param pageSize    页面大小
     * @param currentPage 当前页面
     */

    public  List<T> pageBySubList(List<T> list, int pageSize, int currentPage) {
        int totalCount = list.size();
        int pagecount = 0;
        List<T> subList;
        int m = totalCount % pageSize;
        if (m > 0) {
            pagecount = totalCount / pageSize + 1;
        } else {
            pagecount = totalCount / pageSize;
        }
        if (m == 0) {
            subList = list.subList((currentPage - 1) * pageSize, pageSize * (currentPage));
        } else {
            if (currentPage == pagecount) {
                subList = list.subList((currentPage - 1) * pageSize, pageSize);
            } else {
                subList = list.subList((currentPage - 1) * pageSize, pageSize * (currentPage));
            }
        }
        return subList;
    }
}
